//import { Workbook, Worksheet, Row, BorderStyle, Color, Font, Alignment } from 'excel4node';
import { readFileSync, existsSync, mkdirSync } from 'fs';
import { IDownloadType } from '../kalendjs-type';
import config from './config';
import * as path from 'path';
var xl = require('excel4node');


/**
 * 加载第一个excel 工作表作为模板
 * @param options 
 */
export const loadFirstWorksheetbasicTemp = async function (options: {
    /** 模板相对地址 */
    tempRelativeFileURL: String,
    /** 导出路径 */
    outRelativePath: String,
    /**文件名字 不含格式 如：xxx */
    fileName: String,
    /** 主体 */
    main: (ws: any) => void
}) {
    let wb = new xl.Workbook();
    await wb.xlsx.readFile(path.join(config.filePath.template, `${options.tempRelativeFileURL}`));
    const ws = wb.getWorksheet(1);
    options.main(ws);

    const outPathRoot = path.join(config.filePath.export, `${options.outRelativePath}`); // 文件导出目录
    if (!existsSync(outPathRoot)) {
        mkdirSync(outPathRoot);
    }
    const fileSUF = '.xlsx';// 文件后缀
    const fileURL = `${outPathRoot}\\${Date.now()}${Math.random()}${options.fileName}${fileSUF}`
    await wb.xlsx.writeFile(fileURL);
    return {
        body: readFileSync(fileURL),
        filename: `${options.fileName}${fileSUF}`,
        path: fileURL,
        contentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    } as IDownloadType
}
/**
 * 设置行中每一列的边框
 * @param row 行
 * @param colorARGB 颜色
 * @param style 样式
 */
export const setRowBorder = function (row: any, colorARGB?: String, style?: any, options?: {
    // 是否加粗
    bold?: boolean,
    // 颜色值 默认 应该是红色
    colorArgb?: string,
    // 合并结束行，默认开始结束同一行
    endRow?: any,
    size?: number,
    align?: "left" | "center" | "right" | "fill" | "justify" | "centerContinuous" | "distributed"
}) {
    colorARGB = colorARGB || '000000';
    style = style || 'thin';
    const border = { // 默认边框 实线黑色
        style: style,
        color: { argb: colorARGB } as any
    };
    for (let ci = 1; ci <= row.cellCount; ci++) {
        row.getCell(ci).border = {
            left: border,
            right: border,
            top: border,
            bottom: border
        };
        if (options) {
            row.getCell(ci).font = { name: "宋体", bold: options.bold || true, color: { argb: options.colorArgb || 'b91c22' }, size: options.size || 10, };
            row.getCell(ci).alignment = { horizontal: options.align || 'left' };
        }
    }
}